package com.web.ThirdService.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.web.ThirdService.Service.ThirdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Component
public class consumer {
    @Autowired
    ThirdService thirdService;

    //TODO 注解方式注册交换机和队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.gy.queue", durable = "true", arguments = @Argument(name
                    = "x-queue-mode", value = "lazy")),
            exchange = @Exchange(name = "amp.gy.direct", type = ExchangeTypes.DIRECT),
            key = {"success"}

    ))
    public void listen(String map) throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();
        HashMap hashMap = mapper.readValue(map, HashMap.class);
        Integer itemid = (Integer) hashMap.get("itemid");
        Integer num = (Integer) hashMap.get("num");
        log.info("listener1获取的消息是：{}  ||  解析后的itemid:{},num:{}", map, itemid, num);
//        throw new  RuntimeException("测试");
        thirdService.subtractStock(itemid, num);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "dlx.queue", durable = "true", arguments =
                    @Argument(name = "x-queue-mode", value = "lazy")), exchange = @Exchange(
                    name = "dlx.exchange", type = ExchangeTypes.DIRECT), key = {"dlx"}
            )

    )
    public void listen2(String message) {
        log.info("获取的信息是：{} ", message);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "plugin.queue", durable = "true"),
                    exchange = @Exchange(name = "plugin.exchange", delayed = "true"),
                    key = {"delay"}
            )
    )
    public void listen3(String message) {
        log.info("获取的延时消息:{}",message);
    }
}

